package com.ldk.base.framework.util.baidu;
import java.io.*;
import java.net.*;
import com.alibaba.fastjson.*;

/**
 * 根据经纬度查询省市区
 * @author ycs
 * 2019年4月11日
 * com.ldk.base.framework.util.baidu
 * AddressComponentApi
 */
public class AddressComponentApi {
    /**
     *  "addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"贵州省","city":"遵义市","city_level":2,"district":"红花岗区","town":"","adcode":"520302","street":"延安路","street_number":"86号","direction":"附近","distance":"34"}
     **/
    public static JSONObject getAddressComponent(String  longitude,String latitude,String ak) {
        try {
            String addrJson = geturl("http://api.map.baidu.com/geocoder/v2/?ak="+ak+"&location=" + latitude + "," + longitude + "&output=json&pois=1");
            JSONObject jobjectaddr = JSON.parseObject(addrJson);
            JSONObject rJsonObject = jobjectaddr.getJSONObject("result");
            JSONObject addressComponent = rJsonObject.getJSONObject("addressComponent");
            return  addressComponent;
        } catch (Exception e) {
            return null;
        }
    }

    //{"status":0,"result":{"location":{"lng":106.9423905912411,"lat":27.69758459613015},"formatted_address":"贵州省遵义市红花岗区延安路86号","business":"","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"贵州省","city":"遵义市","city_level":2,"district":"红花岗区","town":"","adcode":"520302","street":"延安路","street_number":"86号","direction":"附近","distance":"34"},"pois":[{"addr":"贵州省遵义市红花岗区延安路41","cp":" ","direction":"附近","distance":"0","name":"招商银行(遵义红花岗支行)","poiType":"金融","point":{"x":106.94239059124112,"y":27.697584610232878},"tag":"金融;银行","tel":"","uid":"624f57a6a459b3f68e7727bf","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"中华北路378号","cp":" ","direction":"东南","distance":"84","name":"上海浦东发展银行(遵义分行)","poiType":"金融","point":{"x":106.94176177738436,"y":27.697960431375888},"tag":"金融;银行","tel":"","uid":"2b1befb68e7afe072e4733bf","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"遵义市红花岗区世贸华廷5楼","cp":" ","direction":"东南","distance":"84","name":"世纪蓝黛俱乐部","poiType":"休闲娱乐","point":{"x":106.94176177738436,"y":27.697960431375888},"tag":"休闲娱乐;ktv","tel":"","uid":"974928e941623235b1077d2b","zip":"","parent_poi":{"name":"世贸华廷","tag":"丽人;美容","addr":"贵州省遵义市红花岗区中华北路378号","point":{"x":106.94224686235958,"y":27.69818432420113},"direction":"南","distance":"76","uid":"39bfd5e4cee8691d72455b0d"}},{"addr":"延安路世贸华廷负一楼","cp":" ","direction":"南","distance":"92","name":"川町太郎(中华北路)","poiType":"美食","point":{"x":106.94232770985545,"y":27.698320258904276},"tag":"美食;外国餐厅","tel":"","uid":"91f99a06a7b0168221f017a5","zip":"","parent_poi":{"name":"世贸华廷","tag":"丽人;美容","addr":"贵州省遵义市红花岗区中华北路378号","point":{"x":106.94224686235958,"y":27.69818432420113},"direction":"南","distance":"76","uid":"39bfd5e4cee8691d72455b0d"}},{"addr":"合众路与延安路交叉口西北50米","cp":" ","direction":"东北","distance":"97","name":"岛内价(中华店)-东南门","poiType":"出入口","point":{"x":106.9418965232108,"y":27.69693691540658},"tag":"出入口;门","tel":"","uid":"de3a16a5e8e92346715e8017","zip":"","parent_poi":{"name":"岛内价(中华店)","tag":"购物;超市","addr":" 贵州省遵义市红花岗区中华路市民广场负一楼","point":{"x":106.94194143848628,"y":27.696832964023338},"direction":"东北","distance":"106","uid":"315c0bccaa3eb68c48fd3f57"}},{"addr":" 贵州省遵义市红花岗区中华路市民广场负一楼","cp":" ","direction":"东北","distance":"106","name":"岛内价(中华店)","poiType":"购物","point":{"x":106.94194143848628,"y":27.696832964023338},"tag":"购物;超市","tel":"","uid":"315c0bccaa3eb68c48fd3f57","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"遵义市红花岗区中华路406号","cp":" ","direction":"东南","distance":"107","name":"遵义市红花岗区国家税务局","poiType":"政府机构","point":{"x":106.94198635376178,"y":27.698368235817467},"tag":"政府机构;行政单位","tel":"","uid":"bc5d77749bd0d64c4657263e","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"遵义市红花岗区合众路14号楼","cp":" ","direction":"西北","distance":"132","name":"聚泉阁社区卫生服务站","poiType":"医疗","point":{"x":106.94347754090785,"y":27.6971448178729},"tag":"医疗;诊所","tel":"","uid":"593850fb2eb1ff5cdf1e5251","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"遵义市红花岗区延安路顺达·帝豪峰景6层","cp":" ","direction":"东北","distance":"143","name":"yulian红征宾馆","poiType":"酒店","point":{"x":106.94172584516396,"y":27.696601072115536},"tag":"酒店;其他","tel":"","uid":"b65ae6813117ca6243afe604","zip":"","parent_poi":{"name":"顺达·帝豪峰景","tag":"房地产;写字楼","addr":"遵义市红花岗区内环路5号","point":{"x":106.94175279432926,"y":27.69648112782993},"direction":"东北","distance":"155","uid":"5ce2637d3cf7cc50ae31e5e6"}},{"addr":"遵义市红花岗区延安路顺达·帝豪峰景11层","cp":" ","direction":"东北","distance":"143","name":"创星发品培训部","poiType":"教育培训","point":{"x":106.94172584516396,"y":27.696601072115536},"tag":"教育培训;培训机构","tel":"","uid":"fa0f1c6a92484aa34354d7d4","zip":"","parent_poi":{"name":"顺达·帝豪峰景","tag":"房地产;写字楼","addr":"遵义市红花岗区内环路5号","point":{"x":106.94175279432926,"y":27.69648112782993},"direction":"东北","distance":"155","uid":"5ce2637d3cf7cc50ae31e5e6"}}],"roads":[],"poiRegions":[],"sematic_description":"招商银行(遵义红花岗支行)附近0米","cityCode":262}}
    private static String geturl(String geturl) throws Exception {
        //请求的webservice的url
        URL url = new URL(geturl);
        //创建http链接
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        //设置请求的方法类型
        httpURLConnection.setRequestMethod("POST");

        //设置请求的内容类型
        httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

        //设置发送数据
        httpURLConnection.setDoOutput(true);
        //设置接受数据
        httpURLConnection.setDoInput(true);

        //发送数据,使用输出流
        OutputStream outputStream = httpURLConnection.getOutputStream();

        //接收数据
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null){
            buffer.append(line);
        }
        String str = buffer.toString();

        return str;
    }
}
